package cn.edu.ctbu.lab.api;

import cn.edu.ctbu.lab.domain.Student;
import cn.edu.ctbu.lab.domain.Teacher;
import cn.edu.ctbu.lab.helper.RUtil;
import cn.edu.ctbu.lab.service.StudentService;
import cn.edu.ctbu.lab.vo.BTPageDO;
import cn.edu.ctbu.lab.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 基本分页
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(value = "/api/student/list")
    public BTPageDO<Student> list(int offset, int limit){

        int pageIndex=offset/limit;
        int pageSize=limit;

        Page<Student> myList=studentService.findByPage(pageIndex,pageSize);

        BTPageDO<Student> stuList=new BTPageDO<Student>();
        stuList.setLimit(pageSize);
        stuList.setOffset(pageIndex);
        stuList.setRows(myList.getContent());
        Long total=myList.getTotalElements();
        stuList.setTotal(total.intValue());

        return  stuList;


    }


    @GetMapping("/api/student/read")
    public R read(Integer id){
        Student student= studentService.findById(id).get();

        return RUtil.success(student);

    }


    @PostMapping(value = "/api/student/add")
    public R add(@Valid Student student, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return RUtil.error(-2,
                    "出错了!"+bindingResult.getFieldError().getDefaultMessage());
        }
        studentService.add(student);

        return RUtil.success();

    }


    @PutMapping(value = "/api/student/update")
    public R update(@Valid Student student, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return RUtil.error(-2,
                    "出错了!"+bindingResult.getFieldError().getDefaultMessage());
        }
        String newPassword=student.getPassword();
        if(newPassword.equals("")){
            //没有 改密码
            Student oldstudent = studentService.findById(student.getId()).get();
            student.setPassword(oldstudent.getPassword());
            //需要使用原来的密码
        }

        studentService.update(student);

        return RUtil.success();

    }


    @DeleteMapping(value = "/api/student/delete")
    public R delete(Integer[] ids){

        Integer mylen= ids.length;
        for (int i = 0; i <mylen ; i++) {

            //读取数据,再删除
            //Student item = studentService.findById(ids[i]).get();

            studentService.delete(ids[i]);


        }

        return RUtil.success();

    }

    @GetMapping(value = "/api/student/query")
    public R quary(String q){


        List<Student> studentList=studentService.findByNameAndPage(q);






        return RUtil.success(studentList);
    }



}